// 刷新验证码
function refreshCaptcha() {
    const timestamp = new Date().getTime();
    document.getElementById('captchaImg').src = `/captcha/get?t=${timestamp}`;
}

// 验证验证码
async function validateCaptcha() {
    const inputCode = document.getElementById('inputCode').value;
    console.log(inputCode)
    try {
        const response = await fetch('/captcha/check', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: `inputCode=${encodeURIComponent(inputCode)}`
        });
        console.log(response)
        const isValid = await response.json();
        console.log(isValid.data)
        if (isValid.data) {
            alert('验证码正确！');
            // 这里可以继续提交表单或其他操作
        } else {
            alert('验证码错误，请重新输入！');
            refreshCaptcha(); // 刷新验证码
            document.getElementById('inputCode').value = ''; // 清空输入框
        }
    } catch (error) {
        console.error('验证请求失败:', error);
        alert('验证失败，请重试！');
    }
}